﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace LinkDownloader.Forms.UserControls
{
    internal class ToolStripSearchTextBox : ToolStripTextBox
    {

        private static StringFormat strinfFormat = new StringFormat() { LineAlignment = StringAlignment.Center };

        public event KeyEventHandler EnterKeyPressed;

        private Color emptyColor = SystemColors.ControlDark;
        public Color EmptyColor
        {
            get { return this.emptyColor; }
            set { this.emptyColor = value; }
        }

        private string emptyText = string.Empty;
        public string EmptyText
        {
            get { return this.emptyText; }
            set { this.emptyText = value; }
        }

        private bool showingEmptyText = true;
        public bool IsShowingEmptyText
        {
            get { return this.showingEmptyText; }
        }

        private bool user = true;
        private Color foreColor = Color.Black;
        public override Color ForeColor
        {
            get { return base.ForeColor; }
            set
            {
                base.ForeColor = value;
                if (this.user) { this.foreColor = value; }
            }
        }

        public ToolStripSearchTextBox()
            : base()
        {
            this.foreColor = this.ForeColor;
            this.CheckStringForEmpty();
        }

        private bool CheckStringForEmpty()
        {
            return (this.showingEmptyText = (this.Text.Length == 0));
        }

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            if (this.showingEmptyText) { this.HideEmpty(); }
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            this.CheckStringForEmpty();
            if (this.showingEmptyText) { this.ShowEmpty(); }
        }

        private void SetForeColor(Color c)
        {
            this.user = false;
            this.ForeColor = c;
            this.user = true;
        }

        public void ShowEmpty()
        {
            this.Text = this.emptyText;
            this.SetForeColor(this.emptyColor);
        }

        public void HideEmpty()
        {
            this.Text = string.Empty;
            this.SetForeColor(this.foreColor);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (e.KeyCode == Keys.Enter) { this.OnEnterKeyPressed(this, e); }
        }

        public virtual void OnEnterKeyPressed(object sender, KeyEventArgs e)
        {
            if (this.EnterKeyPressed != null) { this.EnterKeyPressed(sender, e); }
        }

    }
}
